package friendService

import (
	"chart.zozoo.net/app/models"
	"chart.zozoo.net/core/mysql"
)

// addFriend 添加好友
func addFriend(uid, fid int) (err error) {
	//添加好友逻辑，在好友表中写入两条数据，方便获取好友列表
	//开启事务
	transition := mysql.MysqlClient.Begin()

	//添加数据
	f := new(models.UserFriends)
	f.Uid = uid
	f.Fid = fid
	res := transition.Create(f)
	if res.Error != nil {
		transition.Rollback()
		return res.Error
	}
	f.Uid = fid
	f.Fid = uid
	f.Id = 0
	res = transition.Create(f)
	if res.Error != nil {
		transition.Rollback()
		return res.Error
	}

	//提交事务
	res = transition.Commit()
	if res.Error != nil {
		transition.Rollback()
		return res.Error
	}

	return nil
}
